package com.company.strs;

import com.company.linkedList.ListNode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BasicCalculator {
    public int calculate(String s) {
        //首先去除所有空格

        List<Integer> numbers = new ArrayList<>();
        List<Character> operate = new ArrayList<>();

        String src = s;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < src.length(); i++) {
            char c = s.charAt(i);
            if (c == '+' || c == '-' ||
                    c == '*' || c == '/') {
                numbers.add(Integer.parseInt(builder.toString()));
                operate.add(c);
                builder = new StringBuilder();
            } else if (c == ' ') {
            } else {
                builder.append(c);
            }
        }
        numbers.add(Integer.parseInt(builder.toString()));

        //乘除法优先计算 并且惩乘除法 按顺序计算
        while (operate.contains('*')||operate.contains('/')) {
            //找到第一个预算符号
            int index = 0;
            for (index = 0; index < operate.size(); index++) {
                if (operate.get(index) == '*'|| operate.get(index) == '/') {
                    break;
                }
            }
            Character character = operate.remove(index);
            Integer a = numbers.remove(index);
            Integer b = numbers.get(index);
            if (character == '*') {
                numbers.set(index,a*b);
            }else if (character == '/'){
                numbers.set(index,a/b);
            }
            System.out.println(numbers);
            System.out.println(operate);
        }
        /*加减法顺序计算*/
        while (!operate.isEmpty()){
            Character character = operate.remove(0);
            Integer a = numbers.remove(0);
            Integer b = numbers.get(0);
            if (character == '+') {
                numbers.set(0,a+b);
            }else if (character == '-'){
                numbers.set(0,a-b);
            }
        }
        return numbers.get(0);
    }
}
